"""
按钮模块

该模块提供了用于检测按键状态的类和方法。通过使用这些类和方法，可以轻松地检测按键的短按和长按状态。

模块包含以下类：
- ButtonState: 定义了按键的不同状态。
- Button: 提供了检测按键状态的功能，包括按键按下检测、定时器回调、按键释放处理、按键状态更新和获取按键状态。

示例用法：
button0 = Button(pin=4)  # 按键连接到GPIO引脚4
button1 = Button(pin=5)  # 按键连接到GPIO引脚5

def timer_callback(timer):
    button0.timer_callback()
    button1.timer_callback()

timer = Timer(0)
timer.init(period=10, mode=Timer.PERIODIC, callback=timer_callback)

while True:
    print("button0 当前状态", button0.get_button_state())
    print("button1 当前状态", button1.get_button_state())
    time.sleep(0.5)  # 主程序可以执行其他任务

作者: XSP
创建日期: 2025年4月5日
版本: 1.0.0
"""

from machine import Pin, Timer
import time


class ButtonState:
    NoPress    = 0
    ShortPress = 1
    LongPress = 2

class Button:
    def __init__(self, pin, debounce_time=2, short_press_time=30, long_press_time=100):
        self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.debounce_time = debounce_time
        self.press_time    = 0
        self.short_press_time = short_press_time
        self.long_press_time = long_press_time
        self.button_pressed_time = None
        self.button_pressed_flag = False
        self.state = []
        self.state_temp = 0


    def button_pressed(self):
        """检测按键是否按下"""
        return not self.pin.value()

    def timer_callback(self):
        """定时器回调函数，检测按键状态"""
        if self.button_pressed():
            self.press_time += 1
            if self.press_time > self.debounce_time:
                self.button_pressed_flag = True
        else:
            if self.button_pressed_flag:
                self.process_button_release()
            self.press_time = 0
            self.button_pressed_flag = False

        if self.button_pressed_flag:
            self.update_button_state()

    def process_button_release(self):
        """处理按键释放时的状态"""
        if len(self.state) < 10:
            self.state.append(self.state_temp)
        self.state_temp = ButtonState.NoPress

    def update_button_state(self):
        """更新按键状态"""
        if self.press_time < self.short_press_time:
            self.state_temp = ButtonState.ShortPress  # 短按
        elif self.press_time > self.long_press_time:
            self.state_temp = ButtonState.LongPress  # 长按

    def get_button_state(self):
        """获取按键状态"""
        if self.state:
            return self.state.pop()
        return ButtonState.NoPress



# 示例用法
if __name__ == "__main__":
    import button
    button0 = Button(pin=4)  # 按键连接到GPIO引脚4
    button1 = Button(pin=5)  # 按键连接到GPIO引脚5
    button2 = Button(pin=13)  # 按键连接到GPIO引脚4
    button3 = Button(pin=14)  # 按键连接到GPIO引脚5
   
    def timer_callback(self):
        button0.timer_callback()
        button1.timer_callback()
        button2.timer_callback()
        button3.timer_callback()
    timer = Timer(0)
    timer.init(period=10, mode=Timer.PERIODIC, callback=timer_callback)


    while True:
        time.sleep(0.5)  # 主程序可以执行其他任务
        print("button0 当前状态",button0.get_button_state())
        print("button1 当前状态",button1.get_button_state())
        print("button2 当前状态",button2.get_button_state())
        print("button3 当前状态",button3.get_button_state())


